﻿using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;

public class ClientProtocol : MonoBehaviour
{
    [SerializeField]
    RRClient rRClient;
    struct cmd
    {
        /// <summary>
        /// 投影顶部灯光开关
        /// </summary>
        public const string TouYingLightOnHexStr = "CA 10 02 FA 00 04 FF FF FF FF CA"; //开投影顶部灯光
        public const string TouYingLightOffHexStr = "CA 10 02 FA 00 04 00 00 FF FF CA"; //关投影顶部灯光

        public const string TouYingLightOnXml = "<NVTEKXML><SET><Soft ButtonID='45' /></SET></NVTEKXML>";
        public const string TouYingLightOffXml = "<NVTEKXML><SET><Soft ButtonID='118' /></SET></NVTEKXML>";

        /// <summary>
        /// 电源时序器
        /// </summary>
        public const string PowerOnHexStr = "AE 20 CF 29 03 FF FF 00 AC"; //电源时序器 全开启
        public const string PowerOffHexStr = "AE 20 CF 29 03 FF 00 00 AC"; //电源时序器 全关闭

        /// <summary>
        /// 矩阵切屏
        /// </summary>
        /// 
        public const string QP_old_touying1_Str = "3X3."; //投影矩阵切屏到原电脑
        public const string QP_old_touying2_Str = "3X4."; //投影矩阵切屏到原电脑

        public const string QP_old_Str = "All1."; //矩阵切屏到原电脑
        public const string QP_new1_Str = "2X1."; //矩阵切屏到新电脑
        public const string QP_new2_Str = "4X3."; //矩阵切屏到新电脑


        public const string QP_new_touying1_Str = "4X3."; //矩阵切屏到新电脑
        public const string QP_new_touying2_Str = "4X4."; //投影矩阵切屏到新电脑

        ///// <summary>
        ///// 投影开关指令 (无效)
        ///// </summary>
        //public const string TouYingOnStr = "ky power.on"; //投影开
        //public const string TouYingOffStr = "ky power.off";//投影关

        /// <summary>
        /// 投影开关指令
        /// </summary>
        public const string TouYingOnHexStr = "6B 79 20 70 6F 77 65 72 2E 6F 6E 0D 0A"; //投影开
        public const string TouYingOffHexStr = "6B 79 20 70 6F 77 65 72 2E 6F 66 66 0D 0A";//投影关

        /// <summary>
        /// 开关电视机指令
        /// </summary>
        public const string TVOnHexStr = "25 30 31 23 57 43 50 32 52 32 30 32 30 31 52 31 30 30 30 31 2A 2A 0D 0A";
        public const string TVOffHexStr = "25 30 31 23 57 43 50 32 52 32 30 32 30 30 52 31 30 30 30 31 2A 2A 0D 0A";

        /// <summary>
        /// 灯光模块1 2 3开关指令
        /// </summary>
        public const string LightOn1HexStr = "CA 10 01 FA 00 04 FF FF FF FF CA";
        public const string LightOn2HexStr = "CA 10 02 FA 00 04 FF FF FF FF CA";
        public const string LightOn3HexStr = "CA 10 03 FA 00 04 FF FF FF FF CA";

        public const string LightOff1HexStr = "CA 10 01 FA 00 04 00 00 FF FF CA";
        public const string LightOff2HexStr = "CA 10 02 FA 00 04 00 00 FF FF CA";
        public const string LightOff3HexStr = "CA 10 03 FA 00 04 00 00 FF FF CA";

        public const string LightOnYDYLHexStr = "CA 10 02 08 00 04 02 00 00 01 CA";
        public const string LightOffYDYLHexStr = "CA 10 02 08 00 04 01 00 00 01 CA ";

        /// <summary>
        /// LED 开关指令
        /// </summary>
        public const string LEDOnHexStr = "25 30 31 23 57 43 50 32 52 32 30 32 30 31 52 31 30 30 30 31 2A 2A 0D 0A";
        public const string LEDOffHexStr = "25 30 31 23 57 43 50 32 52 32 30 32 30 30 52 31 30 30 30 31 2A 2A 0D 0A";

        public const string OpenALLPCXML = "<NVTEKXML><SET><Soft ButtonID='3' /></SET></NVTEKXML>";
        public const string CloseALLPCXML = "<NVTEKXML><SET><Soft ButtonID='4' /></SET></NVTEKXML>";
        public const string OpenALLXML = "<NVTEKXML><SET><Soft ButtonID='5' /></SET></NVTEKXML>";
        public const string CloseALLXML = "<NVTEKXML><SET><Soft ButtonID='6' /></SET></NVTEKXML>";
        public const string OpenALLTVXML = "<NVTEKXML><SET><Soft ButtonID='7' /></SET></NVTEKXML>";
        public const string CloseALLTVXML = "<NVTEKXML><SET><Soft ButtonID='8' /></SET></NVTEKXML>";
    }

    public const int sleeptime = 100;


    public void OpenTouYingLight()
    {
        rRClient.SendGBK( cmd.TouYingLightOnXml);
        Thread.Sleep(sleeptime * 2);
        rRClient.SendGBK( cmd.TouYingLightOnXml);
    }

    public void CloseTouYingLight()
    {
        rRClient.SendGBK(cmd.TouYingLightOffXml);
        Thread.Sleep(sleeptime * 2);
        rRClient.SendGBK(cmd.TouYingLightOffXml);
    }

    public void OpenXuTingLED()
    {
        rRClient.SendHex(DataTypeEnum.ZK_LED_XT, OrderTypeEnum.Str, cmd.LEDOnHexStr);
    }

    public void CloseXuTingLED()
    {
        Debug.Log("关闭序厅LED");
        rRClient.SendHex(DataTypeEnum.ZK_LED_XT, OrderTypeEnum.Str, cmd.LEDOffHexStr);
    }

    /// <summary>
    /// 一键开馆
    /// </summary>
    public void KaiGuan()
    {
        OpenAllLight();
        OpenAllCamera();
        OpenAllPC();
        OpenAllTV();

        rRClient.SendGBK(cmd.OpenALLXML);//10中控的开馆指令
    }

    /// <summary>
    /// 一键闭馆
    /// </summary>
    public void BiGuan()
    {
        //CloseAllCamera();
        CloseAllPC();
        //CloseAllTV();
        //CloseAllLight();

        rRClient.SendGBK(cmd.CloseALLXML);//10中控的开馆指令
    }

    /// <summary>
    /// 投影
    /// </summary>
    public void OpenAllCamera()
    {
        Loom.RunAsync(() =>
        {
            openCameraOnce();
            Thread.Sleep(sleeptime);
            openCameraOnce();
        });

    }

    private void openCameraOnce()
    {
        //投影
        OpenTouYing();
        //LED
        //OpenKFZMLED();
        //OpenShaPanTV();
        //OpenXuTingLED();
    }

    private void OpenShaPanTV()
    {
        rRClient.SendHex(DataTypeEnum.ZK_LED_SPBack, OrderTypeEnum.Str, cmd.LEDOnHexStr);
        Thread.Sleep(sleeptime);
    }

    private void OpenKFZMLED()
    {
        rRClient.SendHex(DataTypeEnum.ZK_LED_KFZM_Center, OrderTypeEnum.Str, cmd.LEDOnHexStr);
        Thread.Sleep(sleeptime);
        rRClient.SendHex(DataTypeEnum.ZK_LED_KFZM_Left, OrderTypeEnum.Str, cmd.LEDOnHexStr);
        Thread.Sleep(sleeptime);
        rRClient.SendHex(DataTypeEnum.ZK_LED_KFZM_Right, OrderTypeEnum.Str, cmd.LEDOnHexStr);
        Thread.Sleep(sleeptime);
        rRClient.SendHex(DataTypeEnum.ZK_LED_KFZM_Top, OrderTypeEnum.Str, cmd.LEDOnHexStr);
        Thread.Sleep(sleeptime);
    }

    private void OpenTouYing()
    {
        rRClient.SendHex(DataTypeEnum.ZK_TouYing_Left, OrderTypeEnum.Str, cmd.TouYingOnHexStr);
        rRClient.SendHex(DataTypeEnum.ZK_TouYing_Center, OrderTypeEnum.Str, cmd.TouYingOnHexStr);
        rRClient.SendHex(DataTypeEnum.ZK_TouYing_Right, OrderTypeEnum.Str, cmd.TouYingOnHexStr);
    }

    public void CloseAllCamera()
    {
        Loom.RunAsync(() =>
        {
            closeAllCameraOnce();
            Thread.Sleep(sleeptime);
            closeAllCameraOnce();
        });
    }

    private void closeAllCameraOnce()
    {
        //投影
        //rRClient.SendStr(DataTypeEnum.ZK_TouYing_Left, cmd.TouYingOffStr);
        //rRClient.SendStr(DataTypeEnum.ZK_TouYing_Center, cmd.TouYingOffStr);
        //rRClient.SendStr(DataTypeEnum.ZK_TouYing_Right, cmd.TouYingOffStr);
        CloseTouYing();
        //LED
        //CloseKFZMLED();
        //CloseShaPanTV();
        //CloseXuTingLED();
    }
    /// <summary>
    /// 关闭沙盘背后大屏
    /// </summary>
    private void CloseShaPanTV()
    {
        Debug.Log("关闭沙盘背后大屏");
        rRClient.SendHex(DataTypeEnum.ZK_LED_SPBack, OrderTypeEnum.Str, cmd.LEDOffHexStr);
    }

    private void CloseTouYing()
    {
        Debug.Log("关闭投影");
        rRClient.SendHex(DataTypeEnum.ZK_TouYing_Left, OrderTypeEnum.Str, cmd.TouYingOffHexStr);
        rRClient.SendHex(DataTypeEnum.ZK_TouYing_Center, OrderTypeEnum.Str, cmd.TouYingOffHexStr);
        rRClient.SendHex(DataTypeEnum.ZK_TouYing_Right, OrderTypeEnum.Str, cmd.TouYingOffHexStr);
    }

    public void CloseKFZMLED()
    {
        Debug.Log("关闭开放之门LED");
        rRClient.SendHex(DataTypeEnum.ZK_LED_KFZM_Center, OrderTypeEnum.Str, cmd.LEDOffHexStr);
        rRClient.SendHex(DataTypeEnum.ZK_LED_KFZM_Left, OrderTypeEnum.Str, cmd.LEDOffHexStr);
        rRClient.SendHex(DataTypeEnum.ZK_LED_KFZM_Right, OrderTypeEnum.Str, cmd.LEDOffHexStr);
        rRClient.SendHex(DataTypeEnum.ZK_LED_KFZM_Top, OrderTypeEnum.Str, cmd.LEDOffHexStr);
    }

    /// <summary>
    /// 主机
    /// </summary>
    public void OpenAllPC()
    {
        rRClient.SendHex(DataTypeEnum.ZK_Power, OrderTypeEnum.Str, cmd.PowerOnHexStr);

        rRClient.SendGBK(cmd.OpenALLPCXML);
    }
    public void CloseAllPC()
    {
        Loom.RunAsync(() =>
        {
            rRClient.Send(DataTypeEnum.ShutDown, OrderTypeEnum.ShutDown, "");
            Thread.Sleep(20000);//关机等待20秒关电源
            rRClient.SendHex(DataTypeEnum.ZK_Power, OrderTypeEnum.Str, cmd.PowerOffHexStr);

        });
        //rRClient.SendGBK(cmd.CloseALLPCXML);
    }

    /// <summary>
    /// 一体机
    /// </summary>
    public void OpenAllOneMachine()
    {

    }
    public void CloseAllOneMachine()
    {

    }

    /// <summary>
    /// 电视机 实际为LED屏幕开关
    /// </summary>
    public void OpenAllTV()
    {
        rRClient.SendGBK(cmd.OpenALLTVXML);
    }
    public void CloseAllTV()
    {
        rRClient.SendGBK(cmd.CloseALLTVXML);
    }
    /// <summary>
    /// 灯光
    /// </summary>
    public void OpenAllLight()
    {
        Loom.RunAsync(() =>
        {
            openAllLightOnce();
            Thread.Sleep(sleeptime * 10);
            openAllLightOnce();
        });
    }

    private void openAllLightOnce()
    {
        rRClient.SendHex(DataTypeEnum.ZK_Light, OrderTypeEnum.Str, cmd.LightOn1HexStr);
        Thread.Sleep(sleeptime * 10);
        rRClient.SendHex(DataTypeEnum.ZK_Light, OrderTypeEnum.Str, cmd.LightOn2HexStr);
        Thread.Sleep(sleeptime * 10);
        rRClient.SendHex(DataTypeEnum.ZK_Light, OrderTypeEnum.Str, cmd.LightOn3HexStr);
    }

    public void CloseAllLight()
    {
        Loom.RunAsync(() =>
        {
            closeAllLightOnce();
            Thread.Sleep(sleeptime * 10);
            closeAllLightOnce();
        });

    }

    private void closeAllLightOnce()
    {
        rRClient.SendHex(DataTypeEnum.ZK_Light, OrderTypeEnum.Str, cmd.LightOff1HexStr);
        Thread.Sleep(sleeptime * 10);
        rRClient.SendHex(DataTypeEnum.ZK_Light, OrderTypeEnum.Str, cmd.LightOff2HexStr);
        Thread.Sleep(sleeptime * 10);
        rRClient.SendHex(DataTypeEnum.ZK_Light, OrderTypeEnum.Str, cmd.LightOff3HexStr);
    }

    /// <summary>
    /// 切回旧版本
    /// </summary>
    public void SwitchOld()
    {
        Loom.RunAsync(() =>
        {
            switchOldOnce();
            Thread.Sleep(sleeptime);
            switchOldOnce();
            Debug.Log("切换到旧版完成");
        });
    }

    private void switchOldOnce()
    {
        rRClient.SendStr(DataTypeEnum.QP_KFZM_12, cmd.QP_old_Str);
        Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_KFZM_34, cmd.QP_old_Str);
        Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_KFZM_56, cmd.QP_old_Str);
        Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_KFZM_78, cmd.QP_old_Str);

        Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_TY12, cmd.QP_old_Str);
        Thread.Sleep(sleeptime);
        //rRClient.SendStr(DataTypeEnum.QP_TY3, cmd.QP_old_Str);
        //Thread.Sleep(sleeptime);
        //rRClient.SendStr(DataTypeEnum.QP_TY12, cmd.QP_old_touying1_Str);
        //Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_TY3, cmd.QP_old_touying1_Str);
        Thread.Sleep(sleeptime);
        //rRClient.SendStr(DataTypeEnum.QP_TY12, cmd.QP_old_touying2_Str);
        //Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_TY3, cmd.QP_old_touying2_Str);
        Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_XT, cmd.QP_old_Str);
    }

    /// <summary>
    /// 切到新版本
    /// </summary>
    public void SwitchNew()
    {
        Loom.RunAsync(() =>
        {
            switchNewOnce();
            Thread.Sleep(sleeptime);
            switchNewOnce();
            Debug.Log("切换到新版完成");
        });
    }

    private void switchNewOnce()
    {
        rRClient.SendStr(DataTypeEnum.QP_KFZM_12, cmd.QP_new1_Str);
        Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_KFZM_12, cmd.QP_new2_Str);
        Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_KFZM_34, cmd.QP_new1_Str);
        Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_KFZM_34, cmd.QP_new2_Str);
        Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_KFZM_56, cmd.QP_new1_Str);
        Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_KFZM_56, cmd.QP_new2_Str);
        Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_KFZM_78, cmd.QP_new1_Str);
        Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_KFZM_78, cmd.QP_new2_Str);
        Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_TY12, cmd.QP_new1_Str);
        Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_TY12, cmd.QP_new2_Str);
        Thread.Sleep(sleeptime);
        //rRClient.SendStr(DataTypeEnum.QP_TY3, cmd.QP_new1_Str);
        //Thread.Sleep(sleeptime);
        //rRClient.SendStr(DataTypeEnum.QP_TY3, cmd.QP_new2_Str);
        //Thread.Sleep(sleeptime);
        //rRClient.SendStr(DataTypeEnum.QP_TY12, cmd.QP_new_touying1_Str);
        //Thread.Sleep(sleeptime);
        //rRClient.SendStr(DataTypeEnum.QP_TY12, cmd.QP_new_touying2_Str);
        //Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_TY3, cmd.QP_new_touying1_Str);
        Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_TY3, cmd.QP_new_touying2_Str);
        Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_XT, cmd.QP_new1_Str);
        Thread.Sleep(sleeptime);
        rRClient.SendStr(DataTypeEnum.QP_XT, cmd.QP_new2_Str);
    }

    /// <summary>
    /// 开放之门开门
    /// </summary>
    public void KFZMOpenTheDoor()
    {
        rRClient.SendHex(DataTypeEnum.ZK_KFZM_Door, OrderTypeEnum.Str, "01");
        Thread.Sleep(sleeptime);
        rRClient.SendHex(DataTypeEnum.ZK_KFZM_Door, OrderTypeEnum.Str, "03");
        Thread.Sleep(sleeptime);
        rRClient.SendHex(DataTypeEnum.ZK_KFZM_Door, OrderTypeEnum.Str, "04");
    }
    /// <summary>
    /// 开放之门关门
    /// </summary>
    public void KFZMCloseTheDoor()
    {
        rRClient.SendHex(DataTypeEnum.ZK_KFZM_Door, OrderTypeEnum.Str, "01");
        Thread.Sleep(sleeptime * 2);
        rRClient.SendHex(DataTypeEnum.ZK_KFZM_Door, OrderTypeEnum.Str, "03");
        Thread.Sleep(sleeptime * 2);
        rRClient.SendHex(DataTypeEnum.ZK_KFZM_Door, OrderTypeEnum.Str, "05");
    }
    /// <summary>
    /// 开放之门暂停
    /// </summary>
    public void KFZMPause()
    {
        rRClient.SendHex(DataTypeEnum.ZK_KFZM_Door, OrderTypeEnum.Str, "07");
    }
    /// <summary>
    /// 开放之门继续
    /// </summary>
    public void KFZMResume()
    {
        rRClient.SendHex(DataTypeEnum.ZK_KFZM_Door, OrderTypeEnum.Str, "08");
    }

    public void OnBtns(string str)
    {
        rRClient.Send<string>(DataTypeEnum.LScreenAndRScreen, OrderTypeEnum.SetPlayMovie, str);
    }

    public void OnBtns_PhotoPath(string str)
    {
        print(str);
        rRClient.Send<string>(DataTypeEnum.LScreenAndRScreen, OrderTypeEnum.PhotoPath, str);
    }
}